package Test;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/4/4
 * \* Time: 19:50
 * \* To change this template use File | Settings | File Templates.
 * \* Description:懒汉模式
 * \
 */

class Singleton1{
    //1.不要立即初始化
    public static volatile Singleton1 instance = null;
    //2.把构造方法设为private
    private Singleton1(){}
    //3.提供一个方法来获取以上实例
    public static Singleton1 getInstance(){
        if(instance == null){
            synchronized (Singleton1.class){
                if(instance == null){
                    instance = new Singleton1();
                }
            }
        }
        return instance;
    }
}

public class Demo2 {
    public static void main(String[] args) {
        Singleton1 singleton1 = Singleton1.getInstance();
    }
}
